<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :is-show-before="false">
        <template #before />
        <template #after>
          <el-button @click="exportExcel">导出</el-button>
          <el-button @click="$router.push('/employees/import')">导入</el-button>
          <el-button @click="addEmployeesDialogVisible=true">新增员工</el-button>
        </template>
      </page-tools>

      <!-- 表格 -->
      <!-- 放置表格和分页 -->
      <el-card style="margin: 10px 0;">
        <el-table border :data="employeesList">
          <el-table-column type="index" label="序号" sortable />
          <el-table-column prop="staffPhoto" label="头像" sortable>
            <template slot-scope="{row}">
              <img :src="row.staffPhoto" alt="" width="100%" height="100%" @click="showCode(row.staffPhoto)">
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式" :formatter="formatterType" />
          <el-table-column prop="departmentName" label="部门" sortable />
          <el-table-column label="入职时间" sortable>
            <template slot-scope="{row}">
              {{ row.timeOfEntry |formatTime }}
            </template>
          </el-table-column>
          <el-table-column prop="enableState" label="账户状态" sortable />
          <el-table-column label="操作" sortable fixed="right" width="280">
            <template slot-scope="{row}">
              <el-button type="text" size="small" @click="$router.push('/employees/detail/'+row.id )">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            :page-size="query.size"
            @current-change="onCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 新增员工弹层 -->
      <add-employees :visible.sync="addEmployeesDialogVisible" @add-success="loadEmployeesList" />
      <!-- 显示二维码 -->
      <el-dialog title="二维码" :visible.sync="qrCodeDialogVisible" width="30%">'
        <canvas ref="code" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
// import PageTools from '@/components/PageTools'
import { getAllEmployeesList } from '@/api/employees'
import Employees from '@/api/constant/employees'
import addEmployees from './components/addEmployees.vue'
const { hireType } = Employees
import QRCode from 'qrcode'

export default {
  components: { addEmployees },
  // components: {
  //   PageTools
  // },
  data() {
    return {
      query: {
        page: 1,
        size: 5
      },
      employeesList: [],
      total: 0,
      addEmployeesDialogVisible: false,
      qrCodeDialogVisible: false
    }
  },
  created() {
    this.loadEmployeesList()
  },

  methods: {
    async loadEmployeesList() {
      const res = await getAllEmployeesList(this.query)
      console.log(res)
      this.employeesList = res.rows
      this.total = res.total
    },
    onCurrentChange(val) {
      this.query.page = val
      this.loadEmployeesList()
    },
    formatterType(row, col, cellValue, index) {
      const findItem = hireType.find(item => item.id === cellValue)
      return findItem ? findItem.value : '聘用形式错误'
    },
    exportExcel() {
      import('@/vnder/Export2Excel').then(async excel => {
        const { rows } = await getAllEmployeesList({
          page: 1,
          size: this.total
        })
        const headers = {
          手机号: 'mobile',
          姓名: 'username',
          入职日期: 'timeOfEntry',
          聘用形式: 'formOfEmployment',
          转正日期: 'correctionTime',
          工号: 'workNumber',
          部门: 'departmentName'
        }
        const header = Object.keys(headers)
        const data = rows.map(item => {
          return header.map(item2 => {
            console.log(item2)
            if (item2 === '聘用形式') {
              const findItem = hireType.find(
                item3 => item3.id === item[headers[item2]] - 0
              )
              return findItem ? findItem.value : '未知'
            }
            return item[headers[item2]]
          })
        })
        console.log(data)
        excel.export_json_to_excel({
          header, // 表头 必填tHeader
          data, // 具体数据 必填
          filename: '员工列表', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
      })
    },
    showCode(imgUrl) {
      if (!imgUrl) return this.$message.error('未设置头像')
      this.qrCodeDialogVisible = true
      this.$nextTick(() => {
        QRCode.toCanvas(this.$refs.code, '请付款')
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.body {
  background-color: #fff;
}
</style>
